package com.itheima.health.service.impl;

<<<<<<< HEAD
import com.itheima.health.dao.ReportDao;
import com.itheima.health.pojo.HotSetmeal;
import com.itheima.health.pojo.ReportVO;
import com.itheima.health.service.ReportService;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    private ReportDao reportDao;

    @Override
    public ReportVO getBusinessReportData() {

        ReportVO reportVO=new ReportVO();

        LocalDate reportDate = LocalDate.now();
        LocalDate firstDayOfWeek = reportDate.with(DayOfWeek.MONDAY);
        LocalDate firstDayOfMonth = reportDate.with(TemporalAdjusters.firstDayOfMonth());

        reportVO.setReportDate(reportDate.toString());

        //到诊人数
        Integer todayVisitNumber = reportDao.getVisitNumber(reportDate,reportDate);
        Integer weekVisitNumber = reportDao.getVisitNumber(firstDayOfWeek,reportDate);
        Integer monthVisitNumber = reportDao.getVisitNumber(firstDayOfMonth,reportDate);
        reportVO.setTodayVisitsNumber(todayVisitNumber);
        reportVO.setThisWeekVisitsNumber(weekVisitNumber);
        reportVO.setThisMonthVisitsNumber(monthVisitNumber);

        //会员人数
        Integer todayNewMember = reportDao.getNewMember(reportDate,reportDate);
        Integer weekNewMember = reportDao.getNewMember(firstDayOfWeek,reportDate);
        Integer monthNewMember = reportDao.getNewMember(firstDayOfMonth,reportDate);
        Integer totalMumber = reportDao.getTotalMumber();
        reportVO.setTodayNewMember(todayNewMember);
        reportVO.setThisWeekNewMember(weekNewMember);
        reportVO.setThisMonthNewMember(monthNewMember);
        reportVO.setTotalMember(totalMumber);

        //预约人数
        Integer todayOrderNumber = reportDao.getOrderNumber(reportDate,reportDate);
        Integer weekOrderNumber = reportDao.getOrderNumber(firstDayOfWeek,reportDate);
        Integer monthOrderNumber = reportDao.getOrderNumber(firstDayOfMonth,reportDate);
        reportVO.setTodayOrderNumber(todayOrderNumber);
        reportVO.setThisWeekOrderNumber(weekOrderNumber);
        reportVO.setThisMonthOrderNumber(monthOrderNumber);

        List<HotSetmeal> hotSetmealList =reportDao.getHotSetmeals();
        Integer totalOrderNumber = reportDao.getTotalOrderNumber();

        if (totalOrderNumber!=0) {
            for (HotSetmeal hotSetmeal : hotSetmealList) {
                Double proportion =hotSetmeal.getSetmealCount().doubleValue()/totalOrderNumber;
                hotSetmeal.setProportion(proportion);
            }
        }
        reportVO.setHotSetmeal(hotSetmealList);

        return reportVO;
    }

    @Override
    public void exportBusinessReport(HttpServletResponse response) {
        LocalDate reportDate = LocalDate.now();
        LocalDate firstDayOfWeek = reportDate.with(DayOfWeek.MONDAY);
        LocalDate firstDayOfMonth = reportDate.with(TemporalAdjusters.firstDayOfMonth());

        InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("template/report_template.xlsx");

        try {
            XSSFWorkbook excel=new XSSFWorkbook(inputStream);
            XSSFSheet sheet = excel.getSheet("Sheet1");
            sheet.getRow(2).getCell(5).setCellValue(reportDate.toString());

            //会员数
            sheet.getRow(4).getCell(5).setCellValue(reportDao.getNewMember(reportDate,reportDate));
            sheet.getRow(5).getCell(5).setCellValue(reportDao.getNewMember(firstDayOfWeek,reportDate));
            sheet.getRow(5).getCell(7).setCellValue(reportDao.getNewMember(firstDayOfMonth,reportDate));
            sheet.getRow(4).getCell(7).setCellValue(reportDao.getTotalMumber());

            //预约数
            sheet.getRow(7).getCell(5).setCellValue(reportDao.getOrderNumber(reportDate,reportDate));
            sheet.getRow(8).getCell(5).setCellValue(reportDao.getOrderNumber(firstDayOfWeek,reportDate));
            sheet.getRow(9).getCell(5).setCellValue(reportDao.getOrderNumber(firstDayOfMonth,reportDate));

            //到诊数
            sheet.getRow(7).getCell(7).setCellValue(reportDao.getVisitNumber(reportDate,reportDate));
            sheet.getRow(8).getCell(7).setCellValue(reportDao.getVisitNumber(firstDayOfWeek,reportDate));
            sheet.getRow(9).getCell(7).setCellValue(reportDao.getVisitNumber(firstDayOfMonth,reportDate));

            //热门套餐集合
            List<HotSetmeal> hotSetmealList =reportDao.getHotSetmeals();
            Integer totalOrderNumber = reportDao.getTotalOrderNumber();

            if (totalOrderNumber!=0) {
                for (HotSetmeal hotSetmeal : hotSetmealList) {
                    Double proportion =hotSetmeal.getSetmealCount().doubleValue()/totalOrderNumber;
                    hotSetmeal.setProportion(proportion);
                }
            }

            for (int i = 0; i < hotSetmealList.size(); i++) {
                    sheet.getRow(12+i).getCell(4).setCellValue(hotSetmealList.get(i).getName());
                    sheet.getRow(12+i).getCell(5).setCellValue(hotSetmealList.get(i).getSetmealCount());
                    sheet.getRow(12+i).getCell(6).setCellValue(hotSetmealList.get(i).getProportion());
                    sheet.getRow(12+i).getCell(7).setCellValue(hotSetmealList.get(i).getRemark());
            }

            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename=report.xlsx");
            excel.write(outputStream);

            outputStream.flush();
            outputStream.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

=======
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.pojo.SetmealCount;
import com.itheima.health.service.ReportService;
import com.itheima.health.vo.MemberCountVO;
import com.itheima.health.vo.OrdersettingVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @Date 2023/9/3 11:42
 * @Version 1.0
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Resource
    private MemberDao memberDao;
    @Resource
    private SetmealDao setmealDao;

    /**
     * 获取会员数量信息
     */
    @Override
    public MemberCountVO getMemberReport() {
        MemberCountVO memberCountVO = new MemberCountVO();
        //拿到一年内的每个月份
        List<LocalDate> months = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            months.add(LocalDate.now().minusMonths(i+1));
        }
        //用拿到的月份查询出每月的会员数，并将月份格式化
        List<String> monthss = new ArrayList<>();
        List<Integer> memberCount = new ArrayList<>();
        for (LocalDate month : months) {
            String year = String.valueOf(month.getYear());
            String mon = month.getMonthValue() >= 10 ? String.valueOf(month.getMonthValue()) : "0" + month.getMonthValue();
            monthss.add(year + '.' + mon);
            Integer count = memberDao.getMemberCountByRegTime(month, month.minusMonths(-1));
            memberCount.add(count);
        }
        //封装返回值
        memberCountVO.setMonths(monthss);
        memberCountVO.setMemberCount(memberCount);
        return memberCountVO;
    }

    /**
     * 获取套餐预约信息
     */
    @Override
    public OrdersettingVO getSetmealReport() {
        OrdersettingVO ordersettingVO = new OrdersettingVO();
        //获取所有套餐的名字
        List<String> setmealNames=setmealDao.getName();
        ordersettingVO.setSetmealNames(setmealNames);
        //获取top5 套餐数量信息(套餐名字 + 套餐预约数量)
        List<SetmealCount> setmealCounts = setmealDao.getNemeAndCount();
        ordersettingVO.setSetmealCount(setmealCounts);
        return ordersettingVO;
>>>>>>> origin/dev
    }
}
